package com.siwei.apply.service.impl;

import com.siwei.apply.domain.Gyjsydscdj;
import com.siwei.apply.domain.Project;
import com.siwei.apply.domain.res.GyjsydscdjRes;
import com.siwei.apply.domain.res.GyjsydscdjSinglRes;
import com.siwei.apply.domain.vo.GyjsydscdjUpdateVo;
import com.siwei.apply.domain.vo.GyjsydscdjVo;
import com.siwei.apply.mapper.GyjsydscdjMapper;
import com.siwei.apply.mapper.ProjectMapper;
import com.siwei.apply.service.GyjsydscdjService;
import com.siwei.common.core.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.siwei.apply.common.Common.UserId;

// 新增导入：项目流程相关
import org.springframework.transaction.annotation.Transactional;
import com.siwei.apply.domain.ProjectWorkflow;
import com.siwei.apply.domain.Workflow;
import com.siwei.apply.mapper.ProjectWorkflowMapper;
import com.siwei.apply.mapper.WorkflowMapper;

import java.util.List;
import java.util.Collections;

/**
 * 国有建设用地使用权首次登记 服务实现类
 */
@Service
public class GyjsydscdjImpl implements GyjsydscdjService {
    @Autowired
    private GyjsydscdjMapper gyjsydscdjMapper;
    @Autowired
    private ProjectMapper projectMapper;
    // 新增：项目流程相关Mapper
    @Autowired
    private ProjectWorkflowMapper projectWorkflowMapper;
    @Autowired
    private WorkflowMapper workflowMapper;

    @Override
    public Boolean isExit(String projectId) {
        return gyjsydscdjMapper.isExit(projectId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String add(GyjsydscdjVo gyjsydscdjVo) {
        Gyjsydscdj gyjsydscdj = new Gyjsydscdj();
        BeanUtils.copyProperties(gyjsydscdjVo, gyjsydscdj);
        gyjsydscdj.generateId();
        gyjsydscdj.setCreatorId(UserId);
        gyjsydscdjMapper.add(gyjsydscdj);
        // 添加项目流程（仿照YdbpImpl）
        Integer projectType = projectMapper.getProjectTypeById(gyjsydscdj.getProjectId());
        Workflow workflow = workflowMapper.selectByProjectTypeAndTableName(projectType, "t_gyjsydscdj");
        Workflow workflowPrev = workflowMapper.selectPrevByProjectTypeAndTableName(projectType, "t_gyjsydscdj");
        String parentId = "";
        if (workflowPrev != null) {
            List<ProjectWorkflow> projectWorkflows = projectWorkflowMapper.selectByProjectIdAndNodeTableName(gyjsydscdj.getProjectId(), workflowPrev.getTableName());
            if (projectWorkflows != null && !projectWorkflows.isEmpty()) {
                parentId = projectWorkflows.get(0).getNodeId();
            }
        }
        ProjectWorkflow projectWorkflow = new ProjectWorkflow();
        projectWorkflow.setId(gyjsydscdj.getId());
        projectWorkflow.setParentId(parentId);
        projectWorkflow.setWorkflowId(workflow.getId());
        projectWorkflow.setProjectId(gyjsydscdj.getProjectId());
        projectWorkflow.setNodeId(gyjsydscdj.getId());
        projectWorkflow.setNodeTableName(workflow.getTableName());
        projectWorkflow.setWorkflowName(workflow.getName());
        projectWorkflowMapper.add(projectWorkflow);
        return gyjsydscdj.getId();
    }

    @Override
    public GyjsydscdjRes get(String projectId) {
        List<Gyjsydscdj> list = gyjsydscdjMapper.get(projectId);
        Project project = projectMapper.get(projectId);

        GyjsydscdjRes res = new GyjsydscdjRes();
        res.setProjectId(projectId);
        if (project != null) {
            res.setProjectName(project.getName());
            res.setProjectCode(project.getCode());
        }
        res.setGyjsydscdjList(list);
        return res;
    }

    @Override
    public GyjsydscdjSinglRes getById(String id) {
        GyjsydscdjSinglRes res = new GyjsydscdjSinglRes();
        Gyjsydscdj item = gyjsydscdjMapper.getById(id);
        if (item == null) {
            return null;
        }
        BeanUtils.copyProperties(item,res);

        Project project = projectMapper.get(item.getProjectId());
        res.setProjectId(item.getProjectId());
        if (project != null) {
            res.setProjectName(project.getName());
            res.setProjectCode(project.getCode());
        }
        return res;
    }

    @Override
    public void update(GyjsydscdjUpdateVo gyjsydscdjUpdateVo) {
        gyjsydscdjMapper.update(gyjsydscdjUpdateVo);
    }

    @Override
    public void updateHasOnchain(String id, Boolean hasOnchain) {
        gyjsydscdjMapper.updateHasOnchain(id, hasOnchain);
    }
}
